<%@page import="com.buss.product.model.Goods"%>
<%@page import="com.buss.base.model.DicData"%>
<%@page import="com.buss.product.utils.ProductHelper"%>
<%@page import="com.buss.product.model.CateProp"%>
<%@page import="com.buss.product.model.CateProp.PropSelectType"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<div id="goods_edit" class="idTabs" style="margin-top:4px;margin-left:5px;margin-right:5px;">
	<form:form name="goods_editform" action="" commandName="goods" style="margin:0px;">
		<form:hidden path="id"/>
		<form:hidden path="sortOrder"/>
		<form:hidden path="branchId"/>
		<ul class="tabul">
			<li><a href="#goods_base">基本信息</a></li>
			<li><a href="#goods_prop">商品属性</a></li>		
		</ul>
		<div id="goods_base" style="overflow:auto;height: 435px;">
			
			<table width="100%" align="center" cellpadding="0" cellspacing="8">
			  <tr>
			  	<td align="right">商品编码：</td>
			  	<td><form:input path="code" size="30" cssClass="text"/> </td>
			  	<td align="right"">商品名称：</td>
			  	<td><form:input path="name" size="30" cssClass="text"/> </td>
			  </tr>
			  <tr>
			  	<td align="right"">商品货号：</td>
			  	<td><form:input path="styleNo" size="30" cssClass="text"/> </td>
			  	<td align="right"">拼音简码：</td>
			  	<td><form:input path="pinyinHead" size="30" cssClass="text"/> </td>
			  </tr>
			  <tr>
			  	<td align="right"">拼音全码：</td>
			  	<td><form:input path="pinyin" size="30" cssClass="text"/> </td>
			  	<td align="right"">所属分类：</td>
			  	<td>
			  		<form:hidden path="category.id"/>
			  		<form:input path="category.name" size="30" cssClass="text"/> 
			  	</td>
			  </tr>
			  <tr>
			  	<td align="right"">商品品牌：</td>
			  	<td>
			  		<form:select path="brand.id" itemLabel="brand.name" style="width:188px;">
			  			<form:options items="${brands}" itemLabel="name" itemValue="id"/>
			  		</form:select>
			  	</td>
			  	<td align="right"">库存单位：</td>
			  	<td>
			  		<form:select path="sku" style="width:188px;" cssClass="product_unit">
			  			<form:options items="${dicDatas}" itemLabel="name" itemValue="name" />
			  		</form:select>
			  	</td>
			  </tr>
			  <tr>
			  	<td align="right">入库单位：</td>
			  	<td>
			  		<form:select path="entryUnit" style="width:188px;" cssClass="product_unit">
			  			<form:options items="${dicDatas}" itemLabel="name" itemValue="name" />
			  		</form:select>
			  	</td>
			  	<td align="right">入库转换比例：</td>
			  	<td>
			  		<form:input path="skuToEntryScale" size="30" cssClass="text"/> 
			  	</td>
			  </tr>
			  <tr>
			  	<td align="right">出库单位：</td>
			  	<td>
			  		<form:select path="exUnit" style="width:188px;" cssClass="product_unit">
			  			<form:options items="${dicDatas}" itemLabel="name" itemValue="name" />
			  		</form:select>
			  	</td>
			  	<td align="right">出库转换比例：</td>
			  	<td>
			  		<form:input path="skuToExUnitScale" size="30" cssClass="text"/> 
			  	</td>
			  </tr>
			  <tr>
			  	<!-- <td align="right"">零售单价：</td>
			  	<td><form:input path="price" size="30" cssClass="text"/> </td> -->
			  	<td align="right"">固定价格：</td>
			  	<td>
			  		<form:radiobutton path="isFixed" value="true" label="是"/>
			  		<form:radiobutton path="isFixed" value="false" label="否"/>
			  	</td>
			  	<td align="right"">当前状态：</td>
			  	<td>
			  		<form:radiobutton path="status" label="正常" value="normal" />
					<form:radiobutton path="status" label="不可用" value="disabled"/>
			  	</td>
			  </tr>
			  <tr>
			  	<td align="right">商品描述：</td>
			  	<td colspan="3">
			  		<form:textarea path="intro" rows="7" cols="80" cssClass="textarea"/>
			  	</td>
			  </tr>
			</table>
		</div>
		
		
		<div id="goods_prop" style="overflow: auto;height: 435px;">
			<%
				pageContext.setAttribute("_hasDetail", false);
			%>
			<c:forEach	items="${propGroups}" var="group">
			   <fieldset style="margin-left:10px;margin-right:10px;margin-bottom:10px;">
			   	<legend>${group.displayName }</legend>
			   	<table width="100%" align="center" cellpadding="0" cellspacing="8">
			   		<c:forEach items="${group.props }" var="prop">
			   			<tr>
			   				<td width="80" align="right" valign="top">${prop.name}：</td>
			   				<td>			   	
			   					<!-- 属性为唯一属性 -->			
			   					<c:if test='${prop.selectType == "uniqueProp" && prop.entryWay == "manualWay"}'>
			   						<input type="text" name="${prop.code}" size="30" class="text" value="${goods.propsDataMap[prop.code][0] }"/>
			   					</c:if>
			   					<c:if test='${prop.selectType == "uniqueProp" && prop.entryWay == "listChoice" }'>
			   						<c:forEach items="${prop.listValuesArr }" var="val" varStatus="st">
			   							<c:if test="${goods.propsDataMap[prop.code][0] == val}">
			   								<input type="radio" name="${prop.code }" id="${prop.code}${st.index}" value="${val}" checked="checked"/><label for="${prop.code}${st.index}">${val}</label>
			   							</c:if>
			   							<c:if test="${goods.propsDataMap[prop.code][0] != val}">
			   								<input type="radio" name="${prop.code }" id="${prop.code}${st.index}" value="${val}"/><label for="${prop.code}${st.index}">${val}</label>
			   							</c:if>
			   						</c:forEach>
			   					</c:if>
			   					<c:if test='${prop.selectType == "uniqueProp" && prop.entryWay == "dicChoice" }'>
			   						<%			   			
			   							CateProp prop = (CateProp)pageContext.getAttribute("prop");
			   							String code = prop.getDicCode();
			   							List<DicData> dicDatas = ProductHelper.getDicDataByParentCode(code, request);
			   							int i = 0;
			   							pageContext.setAttribute("__chkbox_sel", false);
			   							for(DicData data : dicDatas){
			   								Goods goods = (Goods)pageContext.findAttribute("goods");
			   								if(goods.getPropsDataMap().get(prop.getCode()) != null){			   									
		   										if (goods.getPropsDataMap().get(prop.getCode())[0].equals(data.getName())){
		   											pageContext.setAttribute("__chkbox_sel", true);
		   										}else{
		   											pageContext.setAttribute("__chkbox_sel", false);
		   										}
			   								}
			   						%>
			   							<c:if test="${__chkbox_sel }">
			   								<input type="radio" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>" checked="checked"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   							<c:if test="${__chkbox_sel == false}">
			   								<input type="radio" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   						<%
			   							i++;
			   							}
			   						%>
			   					</c:if>
			   					
			   					<!-- 属性为单选属性 -->
			   					<c:if test='${prop.selectType == "singleChoice"}'>
			   						<%
			   							pageContext.setAttribute("_hasDetail", true);
			   						%>			   						
			   					</c:if>
			   					<c:if test='${prop.selectType == "singleChoice" && prop.entryWay == "manualWay"}'>
			   						<input type="hidden" class="hiden_singleSel" data="${prop.code}" title="${prop.name}" entryway="manual"/>
			   						<c:if test="${!empty goods.propsDataMap[prop.code]}">
				   						<c:forEach items="${goods.propsDataMap[prop.code]}" var="val" varStatus="propSt">	
				   							<span style="display: block;margin-bottom:2px;">
				   							<input type="text" name="${prop.code}" size="30"class="text single_input" value="${val}" /><c:if test="${!propSt.last}"><a href="javascript:void(0);" class="addSpan" style="margin-left: 5px; display: none;"><span class='bs-button-text icon-add' style='width: 18px;'>&nbsp;</span></a><a href="javascript:void(0);" class="removeSpan" style="margin-left: 5px;"><span class='bs-button-text icon-remove' style='width: 18px;'>&nbsp;</span></a></c:if><c:if test="${propSt.last}"><a href="javascript:void(0);" class="addSpan" style="margin-left: 5px;"><span class='bs-button-text icon-add' style='width: 18px;'>&nbsp;</span></a><c:if test="${propSt.index > 0 }"><a href="javascript:void(0);" class="removeSpan" style="margin-left: 5px;"><span class='bs-button-text icon-remove' style='width: 18px;'>&nbsp;</span></a></c:if><c:if test="${propSt.index == 0 }"><a href="javascript:void(0);" class="removeSpan" style="margin-left: 5px;display:none;"><span class='bs-button-text icon-remove' style='width: 18px;'>&nbsp;</span></a></c:if></c:if></span>			   						
				   						</c:forEach>
			   						</c:if>
			   						<c:if test="${empty goods.propsDataMap[prop.code]}">
			   							<span style="display: block;margin-bottom:2px;">
				   							<input type="text" name="${prop.code}" size="30" class="text single_input"/><a href="javascript:void(0);" class="addSpan" style="margin-left:5px;"><span class='bs-button-text icon-add' style='width:18px;'>&nbsp;</span></a><a href="javascript:void(0);" class="removeSpan" style="margin-left:5px;display:none;"><span class='bs-button-text icon-remove' style='width:18px;'>&nbsp;</span></a> 
				   						</span>
			   						</c:if>
			   					</c:if>		
			   					<c:if test='${prop.selectType == "singleChoice" && prop.entryWay == "listChoice" }'>
			   						<input type="hidden" class="hiden_singleSel" data="${prop.code}" title="${prop.name}" entryway="list"/>
			   						<c:forEach items="${prop.listValuesArr }" var="val" varStatus="st">
			   							<%
			   								CateProp prop = (CateProp)pageContext.getAttribute("prop");
			   								String val = pageContext.getAttribute("val").toString();
			   								Goods goods = (Goods)pageContext.findAttribute("goods");
			   								pageContext.setAttribute("__chkbox_sel", false);
			   								if(goods.getPropsDataMap().get(prop.getCode()) != null){
			   									for(String str : goods.getPropsDataMap().get(prop.getCode())){
			   										if(val.equals(str)){
			   											pageContext.setAttribute("__chkbox_sel", true);
			   											break;
			   										}else{
			   											pageContext.setAttribute("__chkbox_sel", false);
			   											//break;
			   										}
			   									}
			   								}
			   							%>
			   							<c:if test="${__chkbox_sel }">
			   								<input type="checkbox" name="${prop.code}" id="${prop.code }${st.index}" value="${val }" class="single_checkbox" checked="checked"/><label for="${prop.code }${st.index}">${val}</label>
			   							</c:if>
			   							<c:if test="${__chkbox_sel == false }">
			   								<input type="checkbox" name="${prop.code}" id="${prop.code }${st.index}" value="${val }" class="single_checkbox"/><label for="${prop.code }${st.index}">${val}</label>
			   							</c:if>			   							
			   						</c:forEach>
			   					</c:if>	   			
			   					<c:if test='${prop.selectType == "singleChoice" && prop.entryWay == "dicChoice" }'>
			   						<input type="hidden" class="hiden_singleSel" data="${prop.code}" title="${prop.name}" entryway="list"/>
			   						<%			   						
			   							CateProp prop = (CateProp)pageContext.getAttribute("prop");
			   							String code = prop.getDicCode();
			   							List<DicData> dicDatas = ProductHelper.getDicDataByParentCode(code, request);
			   							int i = 0;
			   							pageContext.setAttribute("__chkbox_sel", false);
			   							for(DicData data : dicDatas){
			   								Goods goods = (Goods)pageContext.findAttribute("goods");
			   								if(goods.getPropsDataMap().get(prop.getCode()) != null){
			   									for(String str : goods.getPropsDataMap().get(prop.getCode())){
			   										if(data.getName().equals(str)){
			   											pageContext.setAttribute("__chkbox_sel", true);
			   											break;
			   										}else{
			   											pageContext.setAttribute("__chkbox_sel", false);
			   											//break;
			   										}
			   									}
			   								}
			   						%>
			   							<c:if test="${__chkbox_sel }">
			   								<input type="checkbox" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>"  class="single_checkbox" checked="checked"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   							<c:if test="${__chkbox_sel == false }">
			   								<input type="checkbox" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>" class="single_checkbox"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   						<%
			   								i++;
			   							}
			   						%>
			   					</c:if>	
			   					
			   					<!-- 属性为多选属性 -->
			   					<c:if test='${prop.selectType == "multichoice" && prop.entryWay == "manualWay"}'>
			   						<c:if test="${!empty goods.propsDataMap[prop.code]}">
				   						<c:forEach items="${goods.propsDataMap[prop.code]}" var="val" varStatus="propSt">	
				   							<span style="display: block;margin-bottom:2px;">
					   							<input type="text" name="${prop.code}" size="30" class="text" value="${val}"/><c:if test="${!propSt.last}"><a href="javascript:void(0);" class="addSpan" style="margin-left:2px;display:none;"><span class='bs-button-text icon-add' style='width:18px;'>&nbsp;</span></a></c:if><c:if test="${propSt.last}"><a href="javascript:void(0);" class="addSpan" style="margin-left:5px;"><span class='bs-button-text icon-add' style='width:18px;'>&nbsp;</span></a></c:if><a href="javascript:void(0);" class="removeSpan" style="margin-left:5px;"><span class='bs-button-text icon-remove' style='width:18px;'>&nbsp;</span></a> 
					   						</span>			   						
				   						</c:forEach>
			   						</c:if>
			   						<c:if test="${empty goods.propsDataMap[prop.code]}">
			   							<span style="display: block;margin-bottom:2px;">
				   							<input type="text" name="${prop.code}" size="30" class="text"/><a href="javascript:void(0);" class="addSpan" style="margin-left:5px;"><span class='bs-button-text icon-add' style='width:18px;'>&nbsp;</span></a><a href="javascript:void(0);" class="removeSpan" style="margin-left:5px;"><span class='bs-button-text icon-remove' style='width:18px;'>&nbsp;</span></a> 
				   						</span>
			   						</c:if>
			   					</c:if>	
			   					<c:if test='${prop.selectType == "multichoice" && prop.entryWay == "listChoice" }'>
			   						<c:forEach items="${prop.listValuesArr }" var="val" varStatus="st">
			   							<%
			   								CateProp prop = (CateProp)pageContext.getAttribute("prop");
			   								String val = pageContext.getAttribute("val").toString();
			   								Goods goods = (Goods)pageContext.findAttribute("goods");
			   								pageContext.setAttribute("__chkbox_sel", false);
			   								if(goods.getPropsDataMap().get(prop.getCode()) != null){
			   									for(String str : goods.getPropsDataMap().get(prop.getCode())){
			   										if(val.equals(str)){
			   											pageContext.setAttribute("__chkbox_sel", true);
			   											break;
			   										}else{
			   											pageContext.setAttribute("__chkbox_sel", false);
			   											//break;
			   										}
			   									}
			   								}
			   							%>
			   							<c:if test="${__chkbox_sel }">
			   								<input type="checkbox" name="${prop.code}" id="${prop.code }${st.index}" value="${val }" checked="checked"/><label for="${prop.code }${st.index}">${val}</label>
			   							</c:if>
			   							<c:if test="${__chkbox_sel == false }">
			   								<input type="checkbox" name="${prop.code}" id="${prop.code }${st.index}" value="${val }"/><label for="${prop.code }${st.index}">${val}</label>
			   							</c:if>			   							
			   						</c:forEach>
			   					</c:if>	 
			   					<c:if test='${prop.selectType == "multichoice" && prop.entryWay == "dicChoice" }'>
			   						<%			   						
			   							CateProp prop = (CateProp)pageContext.getAttribute("prop");
			   							String code = prop.getDicCode();
			   							List<DicData> dicDatas = ProductHelper.getDicDataByParentCode(code, request);
			   							int i = 0;
			   							pageContext.setAttribute("__chkbox_sel", false);
			   							for(DicData data : dicDatas){
			   								Goods goods = (Goods)pageContext.findAttribute("goods");
			   								if(goods.getPropsDataMap().get(prop.getCode()) != null){
			   									for(String str : goods.getPropsDataMap().get(prop.getCode())){
			   										if(data.getName().equals(str)){
			   											pageContext.setAttribute("__chkbox_sel", true);
			   											break;
			   										}else{
			   											pageContext.setAttribute("__chkbox_sel", false);
			   											//break;
			   										}
			   									}
			   								}
			   						%>
			   							<c:if test="${__chkbox_sel }">
			   								<input type="checkbox" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>" checked="checked"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   							<c:if test="${__chkbox_sel == false }">
			   								<input type="checkbox" name="${prop.code }" id="${prop.code}<%=i %>" value="<%=data.getName() %>"/><label for="${prop.code}<%=i %>"><%=data.getName() %></label>
			   							</c:if>
			   						<%
			   								i++;
			   							}
			   						%>
			   					</c:if>	
			   				</td>
			   			</tr>
			   		</c:forEach>
				</table>
			   </fieldset>		   
			</c:forEach>				
			
				<fieldset style="margin-left:10px;margin-right:10px;margin-bottom:10px;">
			   		<legend>商品明细</legend>
			   		<div style="display:none;" id="old_details">
			   			<c:forEach items="${goods.details }" var="detail">
			   				<input type="hidden" data="${detail.propContext }" pId="${detail.id }" barcode="${detail.barCode }" price="${detail.price }"/>
			   			</c:forEach>
			   		</div>
			   		
			   		<c:if test="${_hasDetail }">
				   		<table width="100%" align="center" cellpadding="4" cellspacing="0" id="product_details">
				   		
				   		</table>
			   		</c:if>
			   		<c:if test="${_hasDetail == false}">
			   			<table width="100%" align="center" cellpadding="0" cellspacing="8" id="product_details_barcode">
			   				<tr>
			   					<td width="80" align="right" valign="top">商品条码：</td>
			   					<td><input type="text" name="barCode" size="30" class="text" value="${detail.barCode }"/></td>
			   				</tr>
			   				<tr>
			   					<td width="80" align="right" valign="top">销售单价：</td>
			   					<td><input type="text" size="30" name="product_price" class="text" value="${detail.price }"/></td>
			   				</tr>
			   			</table>
			   		</c:if>
			   	</fieldset>
		</div>
	</form:form>
</div>

